package zipcode.Util;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.sql.DataSource;
import org.apache.tomcat.dbcp.dbcp.BasicDataSource;

public class DBUtil {
	private BasicDataSource dataSource;
	private static DBUtil instance;
	private DBUtil(){
		dataSource = new BasicDataSource();
		dataSource.setDriverClassName("oracle.jdbc.driver.OracleDriver");
		dataSource.setUrl("jdbc:oracle:thin:@127.0.0.1:1521:XE");
		dataSource.setUsername("scott2");
		dataSource.setPassword("tiger");
	}
	public static DBUtil getInstance(){
		if(instance == null){
			instance = new DBUtil();
		}
		return instance;
	}
	
	public DataSource getDataSource(){
		return dataSource;
	}
	public void close(PreparedStatement pstmt, Connection conn)throws SQLException{
		if(pstmt!=null){
			pstmt.close();		
		}
		if(conn != null){
			conn.close();
		}
	}
	
	public void close(ResultSet rset, PreparedStatement pstmt, Connection conn)throws SQLException {
		if(rset!=null){
			rset.close();
		}
		close(pstmt, conn);
	}
}
